<?php

/**
 * faktura actions.
 *
 * @package    marketing
 * @subpackage faktura
 * @author     Your name here
 */
class fakturaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
   $date= getdate();
	$date= getdate();
	$default=array("month"=>$date["mon"] , "day"=>$date["mday"],"year"=>$date["year"] );
        
        $date=getdate(mktime(0,0,0, $date["mon"]-1, $date["mday"], $date["year"]));
        $default1=array("month"=>$date["mon"] , "day"=>$date["mday"],"year"=>$date["year"] );
        $filter = array(
            "od" => $default1,
            "do" => $default
	   
        );
        if ($request->hasParameter('id')) {
            $c = new Criteria();
            $c->add(NalogProdajePeer::ID, $request->getParameter('id'));
        }
        else if ($request->hasParameter('faktura_id')) {
            $c = new Criteria();
            $c->add(FakturaPeer::ID, $request->getParameter('faktura_id'));
        }else {
            if ($request->hasParameter('narudzbina')) {


                $this->getUser()->setAttribute('faktura', $request->getParameter('narudzbina'));
                $this->redirect('faktura/index');
            }
            if ($this->getUser()->hasAttribute('faktura')) {
                $filter = $this->getUser()->getAttribute('faktura');
            }
            $c1 = null;
            $c2 = null;
            $c = new Criteria();
            if ($filter['od'] != '') {
                $from = date('Y-m-d', mktime(0, 0, 0, $filter['od']['month'], $filter['od']['day'], $filter['od']['year']));
                $c1 = $c->getNewCriterion(FakturaPeer::DATUM_FAKTURISANJA, $from, Criteria::GREATER_EQUAL);
            }
            if ($filter['do'] != '') {
                $from = date('Y-m-d', mktime(0, 0, 0, $filter['do']['month'], $filter['do']['day'], $filter['do']['year']));
                $c2 = $c->getNewCriterion(FakturaPeer::DATUM_FAKTURISANJA, $from, Criteria::LESS_EQUAL);
            }

            if ($c1 && $c2) {
                $c1->addAnd($c2);
                $c->add($c1);
            } else if ($c1)
                $c->add($c1);
            else if ($c2)
                $c->add($c2);
           
        }
        $this->filter = new NarudzbinaFilterForm();
        unset($this->filter['status']);
        $this->filter->setDefaults($filter);

         $this->filter = new NarudzbinaFilterForm();
        $this->filter->setDefaults($filter);
        unset($this->filter['status']);
        $this->pager = new sfPropelPager('Faktura', 3);
        $this->pager->setMaxPerPage(3);
        $this->pager->setCriteria($c);
        $this->pager->setPeerMethod('doSelectJoinAll');
        $this->pager->setPage($request->hasParameter('page') ? $request->getParameter('page') : 1);
        $this->pager->init();
        $this->faktura_list = $this->pager->getResults();
   
  }

  public function executeNew(sfWebRequest $request)
  {

    $this->form = new FakturaForm();
    $this->form->setDefault('nalog_prodaje_id', $request->getParameter('id'));
    $this->form->setDefault('datum_fakturisanja', date('Y-m-d H:i:s', time()));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new FakturaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

 
  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($faktura = FakturaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object faktura does not exist (%s).', $request->getParameter('id')));
    $this->form = new FakturaForm($faktura);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

 

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $faktura = $form->save();
      $faktura->getNalogProdaje()->getNarudzbina()->setStatusId(5);
      $faktura->getNalogProdaje()->getNarudzbina()->save();
      $this->redirect('faktura/index?faktura_id='.$faktura->getId());
    }
  }
}
